inspector: Show resources
authorMatthias Clasen <mclasen@redhat.com>
Thu, 15 May 2014 23:34:38 +0000 (19:34 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Thu, 15 May 2014 23:36:12 +0000 (19:36 -0400)
Show a list of all registered resources. If a resource looks
like text or an image, we show its content.

gtk/inspector/Makefile.am
gtk/inspector/init.c
gtk/inspector/inspector.gresource.xml
gtk/inspector/resource-list.c [new file with mode: 0644]
gtk/inspector/resource-list.h [new file with mode: 0644]
gtk/inspector/resource-list.ui [new file with mode: 0644]
gtk/inspector/window.ui
po/POTFILES.in

index 2e50840f02535ffb6176cc45144b1dcbde79bd6a..a5c778c12e358e5fbf44d1d0f72e68ddeeb64a97 100644 (file)
@@ -40,6 +40,8 @@ libgtkinspector_la_SOURCES =          \
        python-hooks.c                  \
        python-shell.h                  \
        python-shell.c                  \
+       resource-list.h                 \
+       resource-list.c                 \
        resources.h                     \
        resources.c                     \
        signals-list.h                  \
@@ -84,6 +86,7 @@ templates =                           \
        general.ui                      \
        object-hierarchy.ui             \
        prop-list.ui                    \
+       resource-list.ui                \
        signals-list.ui                 \
        visual.ui                       \
        widget-tree.ui                  \
index 4ea5b759a5af8d07916a96658304b4370425c486..c3cae4f3616d840b79de5676644b6d289506b4f3 100644 (file)
@@ -33,6 +33,7 @@
 #include "prop-list.h"
 #include "python-hooks.h"
 #include "python-shell.h"
+#include "resource-list.h"
 #include "resources.h"
 #include "signals-list.h"
 #include "visual.h"
@@ -58,6 +59,7 @@ gtk_inspector_init (void)
   g_type_ensure (GTK_TYPE_INSPECTOR_PROPERTY_CELL_RENDERER);
   g_type_ensure (GTK_TYPE_INSPECTOR_PROP_LIST);
   g_type_ensure (GTK_TYPE_INSPECTOR_PYTHON_SHELL);
+  g_type_ensure (GTK_TYPE_INSPECTOR_RESOURCE_LIST);
   g_type_ensure (GTK_TYPE_INSPECTOR_SIGNALS_LIST);
   g_type_ensure (GTK_TYPE_INSPECTOR_VISUAL);
   g_type_ensure (GTK_TYPE_INSPECTOR_WIDGET_TREE);
index a9e3f72540580fd874bb657633fe1ef36a756213..427cb02ef33ba9e204b04c88f34a288d80b3ac0d 100644 (file)
@@ -9,6 +9,7 @@
     <file>general.ui</file>
     <file>object-hierarchy.ui</file>
     <file>prop-list.ui</file>
+    <file>resource-list.ui</file>
     <file>signals-list.ui</file>
     <file>visual.ui</file>
     <file>widget-tree.ui</file>
diff --git a/gtk/inspector/resource-list.c b/gtk/inspector/resource-list.c
new file mode 100644 (file)
index 0000000..7727820
--- /dev/null
@@ -0,0 +1,172 @@
+/*
+ * Copyright (c) 2014 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+#include <glib/gi18n-lib.h>
+#include "resource-list.h"
+
+enum
+{
+  COLUMN_NAME,
+  COLUMN_PATH
+};
+
+struct _GtkInspectorResourceListPrivate
+{
+  GtkTreeStore *model;
+  GtkTextBuffer *buffer;
+  GtkWidget *image;
+  GtkWidget *content;
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (GtkInspectorResourceList, gtk_inspector_resource_list, GTK_TYPE_BOX)
+
+static void
+load_resources_recurse (GtkInspectorResourceList *sl,
+                        GtkTreeIter              *parent,
+                        const gchar              *path)
+{
+  gchar **names;
+  gint i;
+  GtkTreeIter iter;
+
+  names = g_resources_enumerate_children (path, 0, NULL);
+  for (i = 0; names[i]; i++)
+    {
+      gint len;
+      gchar *p;
+      gboolean has_slash;
+
+      p = g_strconcat (path, names[i], NULL);
+
+      len = strlen (names[i]);
+      has_slash = names[i][len - 1] == '/';
+
+      if (has_slash)
+        names[i][len - 1] = '\0';
+
+      gtk_tree_store_append (sl->priv->model, &iter, parent);
+      gtk_tree_store_set (sl->priv->model, &iter,
+                          COLUMN_NAME, names[i],
+                          COLUMN_PATH, p,
+                          -1);
+
+      if (has_slash)
+        load_resources_recurse (sl, &iter, p);
+
+      g_free (p);
+    }
+  g_strfreev (names);
+
+}
+
+static void
+selection_changed (GtkTreeSelection         *selection,
+                   GtkInspectorResourceList *rl)
+{
+  GtkTreeIter iter;
+
+  if (gtk_tree_selection_get_selected (selection, NULL, &iter))
+    {
+      gchar *path;
+      GBytes *bytes;
+      gchar *type;
+      gconstpointer data;
+      gsize size;
+      GError *error = NULL;
+
+      gtk_tree_model_get (GTK_TREE_MODEL (rl->priv->model), &iter,
+                          COLUMN_PATH, &path,
+                          -1);
+      if (g_str_has_suffix (path, "/"))
+        {
+          gtk_text_buffer_set_text (rl->priv->buffer, "", -1);
+          gtk_stack_set_visible_child_name (GTK_STACK (rl->priv->content), "text");
+          goto out;
+        }
+      bytes = g_resources_lookup_data (path, 0, &error);
+      if (bytes == NULL)
+        {
+          gtk_text_buffer_set_text (rl->priv->buffer, error->message, -1);
+          g_error_free (error);
+          gtk_stack_set_visible_child_name (GTK_STACK (rl->priv->content), "text");
+        }
+      else
+        {
+          data = g_bytes_get_data (bytes, &size);
+          type = g_content_type_guess (NULL, data, size, NULL);
+          if (g_content_type_is_a (type, "text/*"))
+            {
+              gtk_text_buffer_set_text (rl->priv->buffer, data, -1);
+              gtk_stack_set_visible_child_name (GTK_STACK (rl->priv->content), "text");
+            }
+          else if (g_content_type_is_a (type, "image/*"))
+            {
+              gtk_image_set_from_resource (GTK_IMAGE (rl->priv->image), path);
+              gtk_stack_set_visible_child_name (GTK_STACK (rl->priv->content), "image");
+            }
+          else
+            {
+              gchar *content;
+              content = g_content_type_get_description (type);
+              gtk_text_buffer_set_text (rl->priv->buffer, content, -1);
+              g_free (content);
+              gtk_stack_set_visible_child_name (GTK_STACK (rl->priv->content), "text");
+            }
+          g_free (type);
+          g_bytes_unref (bytes);
+        }
+out:
+      g_free (path);
+    }
+  else
+    {
+      gtk_text_buffer_set_text (rl->priv->buffer, "", -1);
+      gtk_stack_set_visible_child_name (GTK_STACK (rl->priv->content), "text");
+    }
+}
+
+static void
+load_resources (GtkInspectorResourceList *sl)
+{
+
+  load_resources_recurse (sl, NULL, "/");
+}
+
+static void
+gtk_inspector_resource_list_init (GtkInspectorResourceList *sl)
+{
+  sl->priv = gtk_inspector_resource_list_get_instance_private (sl);
+  gtk_widget_init_template (GTK_WIDGET (sl));
+  load_resources (sl);
+}
+
+static void
+gtk_inspector_resource_list_class_init (GtkInspectorResourceListClass *klass)
+{
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/inspector/resource-list.ui");
+  gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, model);
+  gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, buffer);
+  gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, content);
+  gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, image);
+
+  gtk_widget_class_bind_template_callback (widget_class, selection_changed);
+}
+
+// vim: set et sw=2 ts=2:
diff --git a/gtk/inspector/resource-list.h b/gtk/inspector/resource-list.h
new file mode 100644 (file)
index 0000000..2cc0beb
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2014 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _GTK_INSPECTOR_RESOURCE_LIST_H_
+#define _GTK_INSPECTOR_RESOURCE_LIST_H_
+
+#include <gtk/gtk.h>
+
+#define GTK_TYPE_INSPECTOR_RESOURCE_LIST            (gtk_inspector_resource_list_get_type())
+#define GTK_INSPECTOR_RESOURCE_LIST(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj), GTK_TYPE_INSPECTOR_RESOURCE_LIST, GtkInspectorResourceList))
+#define GTK_INSPECTOR_RESOURCE_LIST_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass), GTK_TYPE_INSPECTOR_RESOURCE_LIST, GtkInspectorResourceListClass))
+#define GTK_INSPECTOR_IS_RESOURCE_LIST(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj), GTK_TYPE_INSPECTOR_RESOURCE_LIST))
+#define GTK_INSPECTOR_IS_RESOURCE_LIST_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GTK_TYPE_INSPECTOR_RESOURCE_LIST))
+#define GTK_INSPECTOR_RESOURCE_LIST_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj), GTK_TYPE_INSPECTOR_RESOURCE_LIST, GtkInspectorResourceListClass))
+
+
+typedef struct _GtkInspectorResourceListPrivate GtkInspectorResourceListPrivate;
+
+typedef struct _GtkInspectorResourceList
+{
+  GtkBox parent;
+  GtkInspectorResourceListPrivate *priv;
+} GtkInspectorResourceList;
+
+typedef struct _GtkInspectorResourceListClass
+{
+  GtkBoxClass parent;
+} GtkInspectorResourceListClass;
+
+G_BEGIN_DECLS
+
+GType      gtk_inspector_resource_list_get_type   (void);
+
+G_END_DECLS
+
+#endif // _GTK_INSPECTOR_RESOURCE_LIST_H_
+
+// vim: set et sw=2 ts=2:
diff --git a/gtk/inspector/resource-list.ui b/gtk/inspector/resource-list.ui
new file mode 100644 (file)
index 0000000..a7226b9
--- /dev/null
@@ -0,0 +1,92 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface domain="gtk30">
+  <object class="GtkTreeStore" id="model">
+    <columns>
+      <column type="gchararray"/>
+      <column type="gchararray"/>
+    </columns>
+  </object>
+  <object class="GtkTextBuffer" id="buffer">
+    <property name="text"></property>
+  </object>
+  <template class="GtkInspectorResourceList" parent="GtkBox">
+    <property name="orientation">horizontal</property>
+    <child>
+      <object class="GtkScrolledWindow">
+        <property name="visible">True</property>
+        <property name="expand">True</property>
+        <property name="hscrollbar-policy">automatic</property>
+        <property name="vscrollbar-policy">always</property>
+        <property name="shadow-type">in</property>
+        <child>
+          <object class= "GtkTreeView">
+            <property name="visible">True</property>
+            <property name="model">model</property>
+            <child internal-child="selection">
+              <object class="GtkTreeSelection">
+                <property name="mode">single</property>
+                <signal name="changed" handler="selection_changed"/>
+              </object>
+            </child>
+            <child>
+              <object class="GtkTreeViewColumn">
+                <property name="title" translatable="yes">Path</property>
+                <child>
+                  <object class="GtkCellRendererText">
+                    <property name="scale">0.8</property>
+                  </object>
+                  <attributes>
+                    <attribute name="text">0</attribute>
+                  </attributes>
+                </child>
+              </object>
+            </child>
+          </object>
+        </child>
+      </object>
+    </child>
+    <child>
+      <object class="GtkStack" id="content">
+        <property name="visible">True</property>
+        <child>
+          <object class="GtkScrolledWindow">
+            <property name="visible">True</property>
+            <property name="expand">True</property>
+            <property name="hscrollbar-policy">automatic</property>
+            <property name="vscrollbar-policy">automatic</property>
+            <property name="shadow-type">in</property>
+            <child>
+              <object class="GtkTextView">
+                <property name="visible">True</property>
+                <property name="editable">False</property>
+                <property name="buffer">buffer</property>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="name">text</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkScrolledWindow">
+            <property name="visible">True</property>
+            <property name="expand">True</property>
+            <property name="hscrollbar-policy">automatic</property>
+            <property name="vscrollbar-policy">automatic</property>
+            <property name="shadow-type">in</property>
+            <child>
+              <object class="GtkImage" id="image">
+                <property name="visible">True</property>
+                <property name="halign">center</property>
+                <property name="valign">center</property>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="name">image</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
index 7e2687024b909e2e74cbce340270b3582e828c16..89ec5798086519703c10efc1bb944741b2b6cece 100644 (file)
             <property name="label" translatable="yes">Custom CSS</property>
           </object>
         </child>
+        <child>
+          <object class="GtkInspectorResourceList">
+            <property name="visible">True</property>
+          </object>
+        </child>
+        <child type="tab">
+          <object class="GtkLabel">
+            <property name="visible">True</property>
+            <property name="label" translatable="yes">Resources</property>
+          </object>
+        </child>
         <child>
           <object class="GtkInspectorGeneral">
             <property name="visible">True</property>
index 3aa86b223e25e8e0b7ca35c0414fd9d0b0b60e51..d7f4399d54c19edfe874cf784a874bcb79b9ea9c 100644 (file)
@@ -279,6 +279,7 @@ gtk/inspector/general.ui.h
 gtk/inspector/inspect-button.c
 gtk/inspector/object-hierarchy.ui.h
 gtk/inspector/prop-list.ui.h
+gtk/inspector/resource-path.ui.h
 gtk/inspector/signals-list.c
 gtk/inspector/signals-list.ui.h
 gtk/inspector/visual.ui.h